package com.hsgene.discount.service.impl;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.common.util.validation.FieldChecker;
import com.hsgene.coupon.dto.GeneticUserDiscountDto;
import com.hsgene.discount.persistence.GeneticUserDiscountRepository;
import com.hsgene.discount.service.GeneticUserDiscountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户优惠券ServiceImpl
 *
 * @author wangbing
 * @version 1.0, 2018/10/12
 */
@Service
public class GeneticUserDiscountServiceImpl implements GeneticUserDiscountService {

    @Resource
    private GeneticUserDiscountRepository geneticUserDiscountRepository;

    @Override
    public ApiResult<Pagination<GeneticUserDiscountDto>> queryUserDiscount(String userId, Integer clientDiscountType, OrderHelper orderHelper, LimitHelper limitHelper) {
        FieldChecker.checkEmpty(userId, "用户id");
        FieldChecker.checkEmpty(limitHelper, "分页参数");

        int totalCount = geneticUserDiscountRepository.getUserDiscountCount(userId, clientDiscountType);
        Pagination<GeneticUserDiscountDto> pagination = Pagination.newInstance(limitHelper, totalCount);
        if (totalCount > 0 && limitHelper.getOffset() <= totalCount) {
            pagination.setResult(geneticUserDiscountRepository.queryUserDiscount(userId, clientDiscountType, orderHelper, limitHelper));
        }
        return ApiResult.succ(pagination);
    }
}
